#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>


int main(int argc,char *argv[])
{
    const char *dev_name = "/dev/key";
	int fd = open(dev_name, O_RDWR);
    if(fd < 0)
        printf("Open the device:[%s] failed\n", dev_name);

    struct pollfd fds[1] = 
    {
        [0] = {
            .fd     = fd,         /* file descriptor */
            .events = POLLIN,     /* requested events */
        },
    };
        
    do
    {
        int ret = poll(fds, 1, 3000);
        if(!ret)
        {
            printf("poll timeout\n");
            continue;
        }
        else if(ret < 0)
        {
            printf("poll error\n");
            break;
        }
        
        unsigned char key_val;
        read(fd, &key_val, 1);
        if(key_val & 0x80) //松开
            printf("The key[%u] is released\n", key_val & 0x0f);
        else    //按下
            printf("The key[%u] is pressed\n", key_val & 0x0f);
    }while(1);
	close(fd);
	return 0;
}
